<template>
  <div>
    <div><h1>EL单据信息</h1></div>
    <div>
      <div>
        <el-form :inline="true" :model="formsap" :label-position="right" label-width="100px">
          <el-form-item label="sap单号:">
            <el-input v-model="formsap.sapNumber" class="saptext" type="text"></el-input>
            <el-button type="primary" @click="selectsap" icon="el-icon-searchsousuo">搜索</el-button>
          </el-form-item>
        </el-form>

      </div>
      <div>
        <h2>H表头</h2>
      </div>
      <div class="form-container">
        <el-form :inline="true" v-model="dataGuozhong" :label-position="right" label-width="100px">
          <el-form-item label="交货:">
            <el-input class="jhtext" v-model="dataGuozhong.formdatah.VBELN" type="text"/>
          </el-form-item>
          <el-form-item label="交货类型:">
            <el-input class="jhlxtext" v-model="dataGuozhong.formdatah.LFART" type="text"/>
          </el-form-item>
          <el-form-item label="交货日期:">
            <el-input class="jhrqtext" v-model="dataGuozhong.formdatah.LFDAT" type="text"/>
          </el-form-item>
          <el-form-item label="供应商:">
            <el-input class="gystext" v-model="dataGuozhong.formdatah.LIFNR" type="text"/>
          </el-form-item>
          <el-form-item label="供应商名称:">
            <el-input class="gysmctext" v-model="dataGuozhong.formdatah.NAME1" type="text"/>
          </el-form-item>
          <el-form-item label="司机姓名:">
            <el-input class="sjxmtext" v-model="dataGuozhong.formdatah.ZSJXM" type="text"/>
          </el-form-item>
          <el-form-item label="车号:">
            <el-input class="chtext" v-model="dataGuozhong.formdatah.ZCHEH" type="text"/>
          </el-form-item>
          <el-form-item label="司机电话:">
            <el-input class="sjdhtext" v-model="dataGuozhong.formdatah.ZSJDH" type="text"/>
          </el-form-item>
          <el-form-item label="回传标志:">
            <el-input class="hcbztext" v-model="dataGuozhong.formdatah.ZFLAG" type="text"/>
          </el-form-item>
          <el-form-item label="备注1:">
            <el-input class="bz1text" v-model="dataGuozhong.formdatah.ZHCPH" type="text"/>
          </el-form-item>
          <el-form-item label="备注2:">
            <el-input class="bz2text" v-model="dataGuozhong.formdatah.ZHUOZ" type="text"/>
          </el-form-item>
          <el-form-item label="扣皮重:">
            <el-input class="kpztext" v-model="dataGuozhong.formdatah.ZZKPZ" type="text"/>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>I表头</h2></div>
      <div>
        <el-form :inline="true" v-model="dataGuozhong" :label-position="right" label-width="100px">
          <el-form-item label="交货">
            <el-input class="jhtext" v-model="dataGuozhong.formdatai.VBELN" type="Text"/>
          </el-form-item>
          <el-form-item label="交货项目">
            <el-input class="jhxmtext" v-model="dataGuozhong.formdatai.POSNR" type="Text"/>
          </el-form-item>
          <el-form-item label="物料号">
            <el-input class="wlhtext" v-model="dataGuozhong.formdatai.MATNR" type="Text"/>
          </el-form-item>
          <el-form-item label="物料描述">
            <el-input class="wlmstext" v-model="dataGuozhong.formdatai.MAKTX" type="Text"/>
          </el-form-item>
          <el-form-item label="工厂">
            <el-input class="gctext" v-model="dataGuozhong.formdatai.WERKS" type="Text"/>
          </el-form-item>
          <el-form-item label="工厂名称">
            <el-input class="gcmctext" v-model="dataGuozhong.formdatai.NAME1" type="Text"/>
          </el-form-item>
          <el-form-item label="库存地点">
            <el-input class="kcddtext" v-model="dataGuozhong.formdatai.LGORT" type="Text"/>
          </el-form-item>
          <el-form-item label="库存名称">
            <el-input class="kcmctext" v-model="dataGuozhong.formdatai.LGOBE" type="Text"/>
          </el-form-item>
          <el-form-item label="交货数量">
            <el-input class="jhsltext" v-model="dataGuozhong.formdatai.LFIMG" type="Text"/>
          </el-form-item>
          <el-form-item label="基本计量单位">
            <el-input class="jbjldwtext" v-model="dataGuozhong.formdatai.MEINS" type="Text"/>
          </el-form-item>
          <el-form-item label="采购订单">
            <el-input class="cgddtext" v-model="dataGuozhong.formdatai.VGBEL" type="Text"/>
          </el-form-item>
          <el-form-item label="采购订单项目">
            <el-input class="cgddxmtext" v-model="dataGuozhong.formdatai.VGPOS" type="Text"/>
          </el-form-item>
          <el-form-item label="采购价格">
            <el-input class="cgjgtext" v-model="dataGuozhong.formdatai.NETPR" type="Text"/>
          </el-form-item>
          <el-form-item label="收货库存地点">
            <el-input class="shkcddtext" v-model="dataGuozhong.formdatai.LGORT2" type="Text"/>
          </el-form-item>
          <el-form-item label="收货地点描述">
            <el-input class="shkcddmstext" v-model="dataGuozhong.formdatai.LGOBE2" type="Text"/>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>备注信息</h2></div>
      <div>
        <el-form :inline="true" v-model="dataGuozhong" :label-position="right" label-width="100px">
          <el-form-item label="物流名:">
            <el-input class="wlmtext" v-model="dataGuozhong.formdatac.wlmcc" type="Text"/>
          </el-form-item>
          <el-form-item label="司机身份证">
            <el-input class="sjsfztext" v-model="dataGuozhong.formdatac.sjsfz" type="Text"/>
          </el-form-item>
          <el-form-item label="订单物料数量">
            <el-input class="ddwlsltext" v-model="dataGuozhong.formdatac.ddwlsl" type="Text"/>
          </el-form-item>
          <el-form-item label="原始单号">
            <el-input class="ysdhtext" v-model="dataGuozhong.formdatac.ddysdh" type="Text"/>
          </el-form-item>
          <el-form-item label="原始重量">
            <el-input class="yszltext" v-model="dataGuozhong.formdatac.ddyszl" type="Text"/>
          </el-form-item>
          <el-form-item label="原发日期">
            <el-input class="yfrqtext" v-model="dataGuozhong.formdatac.yfrq" type="Text"/>
          </el-form-item>
          <el-form-item label="原发矿">
            <el-input class="yfktext" v-model="dataGuozhong.formdatac.yfk" type="Text"/>
          </el-form-item>
          <el-form-item label="规格">
            <el-input class="ggtext" v-model="dataGuozhong.formdatac.guige" type="Text"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input class="openid" v-model="dataGuozhong.formdatac.openid" type="Text"/>
          </el-form-item>
          <el-form-item label="超长车标记:">
            <el-checkbox
              v-model="dataGuozhong.formdatac.yc_ccc"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>

          </el-form-item>

        </el-form>
      </div>
      <div>
        <el-button  type="primary"  @click="submitData" >提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>

import {lisTb_mm_h, lisTb_mm_i, tbGuozhongExists, tbGuozhonginsert} from "@/api/system/Fdongbei/zd";


export default {
  data() {
    return {
      right: "",
      formsap: {
        sapNumber: '' // 初始化表单数据
      },
      dataGuozhong: {
        formdatah: {
          VBELN: '',
          LFART: '',
          LFDAT: '',
          LIFNR: '',
          NAME1: '',
          ZSJXM: '',
          ZCHEH: '',
          ZSJDH: '',
          ZFLAG: '',
          ZHCPH: '',
          ZHUOZ: '',
          ZZKPZ: ''
        },
        formdatai: {
          VBELN: '',
          POSNR: '',
          MATNR: '',
          MAKTX: '',
          WERKS: '',
          NAME1: '',
          LGORT: '',
          LGOBE: '',
          LFIMG: '',
          MEINS: '',
          VGBEL: '',
          VGPOS: '',
          NETPR: '',
          LGORT2: '',
          LGOBE2: ''
        },
        formdatac: {
          wlmcc: '',
          ddysdh: '',
          ddwlsl: '',
          ddyszl: '',
          sjsfz: '',
          yfrq: '',
          yfk: '',
          guige: '',
          openid: '',
          yc_ccc: '0'
        },
        guozhong: {
          wlmcc: '',
          jsdj: '',
          card_id: '',
          chehao: '',
          czbh: '',
          wlbh: '',
          wlmc: '',
          gblx: '',
          gysbh: '',
          gysdm: '',
          ckbh: '',
          ckmc: '',
          dbh: '',
          gsbh: '',
          ddxmbh: '',
          gbzt: '',
          sjxm: '',
          sjdh: '',
          sjsfz: '',
          yzkz: '',
          ddwlsl: '',
          ddysdh: '',
          ddyszl: '',
          nlmtbz1: '',
          nlmtbz2: '',
          czymc: '',
          sapbzsc: '',
          yfrq: '',
          yfk: '',
          ddbh: '',
          ymqysj: '',
          guige: '',
          ddysmz: '',
          ddysjz: '',
          fk_zdr: '',
          kk_rjbb: '',
          ycCcc: '0',
          jshth: '',
          fksj: '',
          ysmz_bj: '',
          chexc: ''
        }
      }
    }
      ;
  },
  methods: {
    selectsap() {

      try {
        lisTb_mm_h({sap: this.formsap.sapNumber,lfart:"EL"}).then(response => {
          if(response.data[0]===undefined){
            this.$modal.alertWarning("Tb_mm_h缺失")
          }
          const data = response.data[0];
          //  this.formdatah =data;
          this.dataGuozhong.formdatah = {
            VBELN: data.vbeln,
            LFART: data.lfart,
            LFDAT: data.lfdat,
            LIFNR: data.lifnr,
            NAME1: data.name1,
            ZSJXM: data.zsjxm,
            ZCHEH: data.zcheh,
            ZSJDH: data.zsjdh,
            ZFLAG: data.zflag,
            ZHCPH: data.zhcph,
            ZHUOZ: data.zhuoz,
            ZZKPZ: data.zzkpz
          };
        });
      } catch (error) {
        console.error('请求失败:', error);
      }

      try {
        lisTb_mm_i({sap: this.formsap.sapNumber}).then(response => {
          if(response.data[0]===undefined){
            this.$modal.alertWarning("Tb_mm_i缺失")
          }
          const data = response.data[0];
          //  this.formdatah =data;
          this.dataGuozhong.formdatai = {
            VBELN: data.vbeln,
            POSNR: data.posnr,
            MATNR: data.matnr,
            MAKTX: data.maktx,
            WERKS: data.werks,
            NAME1: data.name1,
            LGORT: data.lgort,
            LGOBE: data.lgobe,
            LFIMG: data.lfimg,
            MEINS: data.meins,
            VGBEL: data.vgbel,
            VGPOS: data.vgpos,
            NETPR: data.netpr,
            LGORT2: data.lgort2,
            LGOBE2: data.lgobe2
          };

        });
      } catch (error) {
        console.error('请求失败:', error);
      }

    },
    submitData() {//提交
      console.log(this.dataGuozhong.formdatac.yc_ccc)
      if (this.dataGuozhong.formdatah.ZCHEH === "") {
        this.qk()
        this.$modal.alertWarning("车号不能为空")
        return
      }

      tbGuozhongExists({chehao: this.dataGuozhong.formdatah.ZCHEH}).then(response => {


        if (response.msg === 'OK') {
          this.guozhong = {
            wlmcc: this.dataGuozhong.formdatac.wlmcc,
            jsdj: this.dataGuozhong.formdatai.NETPR,
            card_id: "",
            chehao: this.dataGuozhong.formdatah.ZCHEH,
            czbh: "DR",
            wlbh: this.dataGuozhong.formdatai.MATNR,
            wlmc: this.dataGuozhong.formdatai.MAKTX,
            gblx: "EL",
            gysbh: this.dataGuozhong.formdatah.LIFNR,
            gysdm: this.dataGuozhong.formdatah.NAME1,
            ckbh: this.dataGuozhong.formdatai.LGORT,
            ckmc: this.dataGuozhong.formdatai.LGOBE,
            dbh: "",
            gsbh: this.dataGuozhong.formdatai.WERKS,
            ddxmbh: this.dataGuozhong.formdatai.VGPOS,
            gbzt: "待进厂",
            sjxm: this.dataGuozhong.formdatai.ZSJXM,
            sjdh: this.dataGuozhong.formdatai.ZSJDH,
            sjsfz: this.dataGuozhong.formdatac.sjsfz,
            yzkz: this.dataGuozhong.formdatac.ZZKPZ,
            ddwlsl: this.dataGuozhong.formdatai.LFIMG,
            ddysdh: this.dataGuozhong.formdatac.ddysdh,
            ddyszl: this.dataGuozhong.formdatac.ddyszl,
            nlmtbz1: this.dataGuozhong.formdatai.ZHCPH,
            nlmtbz2: this.dataGuozhong.formdatai.ZHUOZ,
            czymc: this.$store.state.user.name,
            sapbzsc: this.dataGuozhong.formdatai.ZFLAG,
            yfrq: this.dataGuozhong.formdatac.yfrq,
            yfk: this.dataGuozhong.formdatac.yfk,
            ddbh: this.dataGuozhong.formdatai.VBELN,
            ymqysj: "",
            guige: this.dataGuozhong.formdatac.guige,
            ddysmz: "",
            ddysjz: "",
            fk_zdr: this.$store.state.user.name,
            kk_rjbb: "",
            ycCcc: this.dataGuozhong.formdatac.yc_ccc,
            jshth: "",
            fksj: new Date().toISOString(),
            ysmz_bj: "",
            chexc: ""
          }
       //   console.log("开始访问后端")
          tbGuozhonginsert(this.guozhong).then(response => {
            if (response.msg != 0) {
              this.qk()
              this.$modal.alert("保存成功")

            } else {
              this.qk()
              this.$modal.alertWarning("保存失败")

            }
          })
        } else {
          this.qk()
          this.$modal.alertWarning(response.data)
        }
      })

    },
    qk() {
      this.formsap = {
        sapNumber: '' // 初始化表单数据
      },
        this.dataGuozhong = {
          formdatah: {
            VBELN: '',
            LFART: '',
            LFDAT: '',
            LIFNR: '',
            NAME1: '',
            ZSJXM: '',
            ZCHEH: '',
            ZSJDH: '',
            ZFLAG: '',
            ZHCPH: '',
            ZHUOZ: '',
            ZZKPZ: ''
          },
          formdatai: {
            VBELN: '',
            POSNR: '',
            MATNR: '',
            MAKTX: '',
            WERKS: '',
            NAME1: '',
            LGORT: '',
            LGOBE: '',
            LFIMG: '',
            MEINS: '',
            VGBEL: '',
            VGPOS: '',
            NETPR: '',
            LGORT2: '',
            LGOBE2: ''
          },
          formdatac: {
            wlmcc: '',
            ddysdh: '',
            ddwlsl: '',
            ddyszl: '',
            sjsfz: '',
            yfrq: '',
            yfk: '',
            guige: '',
            yc_ccc: '0'
          },
          guozhong: {
            wlmcc: '',
            jsdj: '',
            card_id: '',
            chehao: '',
            czbh: '',
            wlbh: '',
            wlmc: '',
            gblx: '',
            gysbh: '',
            gysdm: '',
            ckbh: '',
            ckmc: '',
            dbh: '',
            gsbh: '',
            ddxmbh: '',
            gbzt: '',
            sjxm: '',
            sjdh: '',
            sjsfz: '',
            yzkz: '',
            ddwlsl: '',
            ddysdh: '',
            ddyszl: '',
            nlmtbz1: '',
            nlmtbz2: '',
            czymc: '',
            sapbzsc: '',
            yfrq: '',
            yfk: '',
            ddbh: '',
            ymqysj: '',
            guige: '',
            ddysmz: '',
            ddysjz: '',
            fk_zdr: '',
            kk_rjbb: '',
            ycCcc: '0',
            jshth: '',
            fksj: '',
            ysmz_bj: '',
            chexc: ''
          }
        }
    }
  }
};

</script>

<style scoped lang="scss">
.el-icon-searchsousuo {
  display: flex;
  align-items: center;
}

.el-input {
  width: auto;
  flex-grow: 3;
}

.el-button {
  margin-left: 10px; /* 调整按钮与输入框之间的间距 */
  flex-shrink: 0; /* 防止按钮在宽度不足时被压缩 */
}
</style>
